function $$() {
  return $A(arguments).map(function(expression) {
        return expression.strip().split(/\s+/).inject([null], function(results, expr) {
          var selector = new Selector(expr);
          return results.map(selector.findElements.bind(selector)).flatten();
        });
   }).flatten();
}

function userauth_init(){
	if ($('msg')) {
		Effect.Pulsate ('msg');
		new Effect.Highlight ('msg');
	}
	if ($('groupmsg')) {
		Effect.Pulsate ('groupmsg');
		new Effect.Highlight ('groupmsg');
	}
}

function toggleUsers() {
	Effect.toggle ('usersholder', 'Blind', {duration:0.4});
}

function toggleGroups() {
	Effect.toggle ('groupsholder', 'Blind', {duration:0.4});
}

function addUser() {
	Effect.toggle ('addUser', 'Blind', {duration:0.4});	
}

function addGroup() {
	Effect.toggle ('addGroup', 'Blind', {duration:0.4});	
}

function checkMail(email) {
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(email)) {return true;}
	else {return false;}
}

function stripe () {
  var tables = document.getElementsByTagName("table");  

  for(var x=0;x!=tables.length;x++){
    var table = tables[x];
	// if it isn't a table, or if it lacks a stripe class, skip it
	if (! table || document.getElementsByClassName("stripe")=="") { return; } 
	
    var tbodies = table.getElementsByTagName("tbody");
    
    for (var h = 0; h < tbodies.length; h++) {
		
	  var even = true;
      var trs = tbodies[h].getElementsByTagName("tr");
      
      for (var i = 0; i < trs.length; i++) {
        trs[i].onmouseover=function(){
          this.className += " ruled"; return false
        }
        trs[i].onmouseout=function(){
          this.className = this.className.replace("ruled", ""); return false
        }
        
        if(even)
          trs[i].className += " even";
        
        even = !even;
      }
    }
  }
}

function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}

var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();

function addRowToTable(tablename)
{
  var tbl = document.getElementById(tablename);
  var lastRow = tbl.rows.length;
  // if there's no header row in the table, then iteration = lastRow + 1
  var iteration = lastRow;
  var row = tbl.insertRow(lastRow);
  
  var username = row.insertCell(0);
  var group = row.insertCell(1);
  var fullname = row.insertCell(2);
  var email = row.insertCell(3);
  var password = row.insertCell(4);
  var enabled = row.insertCell(5);
  var edit_delete = row.insertCell(6);
}


addEvent(window,'unload',EventCache.flush);
addEvent (window, "load", stripe);
addEvent (window, "load", userauth_init);